<template>
  <div>
    <!-- 1.0搜索框 -->
    <my-search level="添加商品" @addSearch="getQuery" @myadd="addGoods"></my-search>
    <!-- 2.0商品列表 -->
    <el-table :data="goodlist" border stripe style="width: 100%">
      <el-table-column label="编号" type="index" width="50"></el-table-column>
      <el-table-column prop="goods_name" label="名称" width="350"></el-table-column>
      <el-table-column prop="goods_price" label="价格" width="140"></el-table-column>
      <el-table-column prop="goods_number" label="库存量" width="130"></el-table-column>
      <el-table-column label="商品状态" width="100">
        <template slot-scope="scope">{{scope.row.goods_state |filterState}}</template>
      </el-table-column>
      <el-table-column label="创建时间" width="230">
        <!-- 用过滤器来格式化 -->
        <template slot-scope="scope">{{scope.row.add_time | dateFilter('/')}}</template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            icon="el-icon-edit"
            type="primary"
            size="mini"
            plain
             @click="$router.push({path:'/layout/goods/editgoods?goods_id='+scope.row.goods_id})"
          ></el-button>
          <el-button
            size="mini"
            icon="el-icon-delete"
            type="danger"
            @click="deleteGoodsList(scope.row.goods_id)"
            plain
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 3.0分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagenum"
      :page-sizes="[10, 500, 100, 500]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>
<script>
//导入搜索框子组件
import Mysearch from '../subcomponents/Mysearch'
export default {
  //注册搜索框子组件
  components: {
    'my-search': Mysearch
  },
  data() {
    return {
      goodlist: [], //商品列表数据
      query: '', //添加关键字
      pagenum: 1, //当前页码
      pagesize: 10, //页容量
      total: this.total //总条数
    }
  },
  //过滤器
  filters: {
    /**
     * 过滤器的第一个参数，是 | 前面的原始数据
     */
    filterState(input) {
      let state = ''
      switch (input) {
        case 0:
          state = '未通过'
          break
        case 1:
          state = '审核中'
          break
        case 2:
          state = '已审核'
          break
      }

      return state
    }
  },
  created() {
    this.getGoodsListData()
  },
  methods: {
    //获取商品列表数据
    getGoodsListData() {
      this.$axios
        .get(`goods`, {
          params: {
            pagenum: this.pagenum,
            pagesize: this.pagesize,
            query: this.query
          }
        })
        .then(res => {
          //赋值给模型
          this.goodlist = res.data.data.goods
          //获取总条数
          this.total = res.data.data.total
        })
    },
    //当页面发生改变的时候触发
    handleSizeChange(pagesize) {
      this.pagesize = pagesize
      this.getGoodsListData()
    },
    //当前页发生改变的时候触发
    handleCurrentChange(pagenum) {
      this.pagenum = pagenum
      this.getGoodsListData()
    },
    //接收子组件传过来的关键字数据
    getQuery(query) {
      this.query = query
      this.getGoodsListData()
    },
    //添加商品 实现跳转
    addGoods() {
      this.$router.push({ path: '/layout/goods/addgoods' })
    },

    //删除商品列表
     deleteGoodsList(goods_id){
       console.log(goods_id)
      this.$confirm('确定删除该商品吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
        this.$axios.delete(`goods/${goods_id}`).then(res=>{
          if (res.data.meta.status === 200){
            // 提示
            this.$message({
                message: res.data.meta.msg,
                type: 'success'
            })

            // 刷新
            this.getGoodsListData()
          }
        })
      }).catch(() => {
              
      });
    }
  }
}
</script>
<style>
.search {
  width: 300px;
}
</style>

